﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace rkill {
    class RegionKiller {
        internal static void KillRegionsBelow(string path) {
            string[] csFiles = Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories);
            foreach (string file in csFiles) {
                string[] lines = File.ReadAllLines(file);
                var cleanLinesQuery = from line in lines where !(line.TrimStart().StartsWith("#region") || line.TrimStart().StartsWith("#endregion")) select line;

                string[] cleanLines = cleanLinesQuery.ToArray();
                int regionCount = lines.Length - cleanLines.Length;
                if (regionCount > 0) {
                    int regions = regionCount / 2;
                    Logger.VerboseLine(String.Format("Killing {0} region{1} in {2}", regions, regions > 1?"s":"", file));
                    Effects.RegionsKilled += regions;
                    Effects.FilesAltered++;

                    File.WriteAllLines(file, cleanLines);
                }
            }
        }
    }
}
